package ${filePackageName};

import ${utilsPackage}.ConstantUtil;
import ${commonBeanPackage}.ApiResult;
import ${utilsPackage}.MessageUtils;
import ${enumsPackage}.EnumCode;
import ${utilsPackage}.FileUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import org.springframework.http.MediaType;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 拦截器
 * @author ${projectAuthor}
 * @version 1.0
 * @date ${nowDate}
 */
public class ManageInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object obj) throws Exception {

        boolean flag = true;
        String token = request.getHeader(ConstantUtil.DEFAULT_AUTH_TOKEN);
        if(ObjectUtil.isEmpty(token)){
            //响应用户未登录
            FileUtil.output(response, JSON.toJSONString(ApiResult.authorizationDeny(MessageUtils.get(EnumCode.USER_NO_LOGIN))), MediaType.APPLICATION_JSON_VALUE);
            return false;
        }

        return flag;
    }


    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object obj, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object obj, Exception e) throws Exception {

    }
}
